package net.huashitong.Indicators.controller;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.Indicators.entity.Industry;
import net.huashitong.Indicators.service.ImportDataProcess;
import net.huashitong.Indicators.service.IndustryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.sound.midi.Instrument;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

@Controller
@RequestMapping(value = "data/importdata")
public class ImportDateController extends JsonBaseController {

    static final String path = "D:/data/xiamenDataZhengxinyun/";

    @Override
    protected String getView(String s) {
        return null;
    }

    @Autowired
    private ImportDataProcess importDataProcess;

    @Autowired
    private IndustryService industryService;


    /**
     * 上传页面
     *
     * @return
     */
    @RequestMapping(value = "/index")
    public String indexImport() {
        return "/modules/importIndex";
    }


    @ResponseBody
    @RequestMapping(value = "/import")
    public Message importData(@RequestParam(value = "file") MultipartFile file) {

        if (file.getOriginalFilename().endsWith(".xlsx") == false) {
            return coverMessage("500", "文件后缀不对");
        }
        String filePath;
        try {
            write(file.getOriginalFilename(), file.getInputStream());
            filePath = path + file.getOriginalFilename();
            filePath = new String(filePath.getBytes("utf-8"), "utf-8");
            if (StringUtils.isBlank(filePath)) {
                return coverMessage("500", "filePath为空");
            }

            List<Industry> list = importDataProcess.importIndustry(filePath);
            industryService.batchSave(list);

            return coverMessage("200", "导入成功！");
        } catch (Exception e) {
            return coverMessage("500", "导入失败！", e.getMessage());
        }
    }


    private static void write(String filename, InputStream in) {
        File file = new File(path);
        if (!file.exists()) {
            if (!file.mkdirs()) {// 若创建文件夹不成功
                System.out.println("Unable to create external cache directory");
            }
        }
        File targetfile = new File(path + filename);
        OutputStream os = null;
        try {
            os = new FileOutputStream(targetfile);
            int ch = 0;
            while ((ch = in.read()) != -1) {
                os.write(ch);
            }
            os.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                os.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


}
